//
//  NetworkTools.swift
//  Swift
//
//  Created by 贺瑞 on 2017/6/24.
//  Copyright © 2017年 herui. All rights reserved.
//

import AFNetworking
import SVProgressHUD


class NetWorkTools: AFHTTPSessionManager {
    
    //单例
    static let sharedInstance : NetWorkTools = {
        let tools = NetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
    }()
    
    //发送post请求
    static func post(url : String, params : Any?, finish : @escaping (_ resp : Any?, _ error : Error?)->()) {
        
        SVProgressHUD.show()
        NetWorkTools.sharedInstance.post(url, parameters: params, progress: nil, success: { (task : URLSessionDataTask, resp : Any?) in
            SVProgressHUD.dismiss()
            finish(resp, nil)
        }, failure: { (task : URLSessionDataTask?, error : Error) in
            SVProgressHUD.dismiss()
            finish(nil, error)
        })
    }
    
    //发送get请求
    static func get(url : String, params : Any?, finish : @escaping (_ resp : Any?, _ error : Error?)->()) {
        
        SVProgressHUD.show()
        NetWorkTools.sharedInstance.get(url, parameters: params, progress: nil, success: { (task : URLSessionDataTask, resp : Any?) in
            SVProgressHUD.dismiss()
            finish(resp, nil)
        }) { (task : URLSessionDataTask?, error : Error) in
            SVProgressHUD.dismiss()
            finish(nil, error)
        }
    }
    
    
}
